package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.entity.vo.ecologypool.*;
import com.sc.nft.enums.EcologyPoolTypeEnum;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.EcologyPoolService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 刘国航
 * @since 2024/7/13
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ecologyPool")
@Api(tags = "生态池")
public class EcologyPoolController {

    private final EcologyPoolService ecologyPoolService;

    @ApiOperation(value = "获取生态池积分", notes = "获取生态池积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getPoolData", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<EcologyPoolDataNftVO>> getPoolData(@Valid @RequestBody EcologyPoolDataRequest request) {
        Map<EcologyPoolTypeEnum, EcologyPoolDataVO> ecologyPoolDataMap = ecologyPoolService.getPoolData(request.getTypeList(), request.getDaoId(), UserPool.getUser());
        if (Objects.isNull(ecologyPoolDataMap) || ecologyPoolDataMap.isEmpty()) {
            return Result.success();
        }
        List<EcologyPoolDataNftVO> ecologyPoolDataNftVOList = ecologyPoolDataMap.values().stream().map(ecologyPoolData -> {
            EcologyPoolDataNftVO ecologyPoolDataNftVO = new EcologyPoolDataNftVO();
            ecologyPoolDataNftVO.setType(ecologyPoolData.getType());
            ecologyPoolDataNftVO.setLinkCount(String.valueOf(ecologyPoolData.getLinkCount()));
            ecologyPoolDataNftVO.setLinkType(ecologyPoolData.getLinkType());
            ecologyPoolDataNftVO.setLinkTypeName(ecologyPoolData.getLinkTypeName());
            ecologyPoolDataNftVO.setPoolName(ecologyPoolData.getPoolName());
            return ecologyPoolDataNftVO;
        }).collect(Collectors.toList());
        return Result.success(ecologyPoolDataNftVOList);
    }

    @ApiOperation(value = "品牌贡献榜配置", notes = "品牌贡献榜配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoRankConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<EcologyPoolWebRankConfVO> daoRankConf() {
        return Result.success(ecologyPoolService.daoRankConf());
    }

    @ApiOperation(value = "历史品牌贡献榜配置详情", notes = "历史品牌贡献榜配置详情-玛特DAO", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/historyDaoRankConfDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<EcologyPoolHistoryDaoRankConfVo> historyDaoRankConfDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolService.historyDaoRankConfDetail(request.getId()));
    }

    @ApiOperation(value = "品牌贡献榜", notes = "品牌贡献榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoRankPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolWebDaoRankVO> daoRankPage(@RequestBody PageRequest request) {
        return PageResult.success(ecologyPoolService.daoRankPage(request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "流水记录", notes = "流水记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/recordPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolWebRecordVO> recordPage(@RequestBody EcologyPoolRequest request) {
        return PageResult.success(ecologyPoolService.webRecordPage(request.getPageNo(), request.getPageSize(), request.getPoolType(), request.getDaoId(), UserPool.getUser()));
    }

    @ApiOperation(value = "历史排行榜", notes = "历史排行榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/historyDaoRankConfPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolWebHistoryDaoRankConfVO> historyDaoRankConfPage(@RequestBody PageRequest request) {
        return PageResult.success(ecologyPoolService.historyDaoRankConfPage(request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "历史排行榜详情", notes = "历史排行榜详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/historyDaoRankPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolWebHistoryRankVO> historyDaoRankPage(@RequestBody IdPageRequest request) {
        return PageResult.success(ecologyPoolService.historyDaoRankPage(request.getPageNo(), request.getPageSize(), request.getId()));
    }

    @ApiOperation(value = "服务中心/社区网点排行榜", notes = "服务中心/社区网点排行榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/communityServiceRankConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<EcologyPoolWebRankConfVO> communityServiceRankConf(@RequestBody @Valid EcologyPoolCommunityServiceRankConfRequest request) {
        return Result.success(ecologyPoolService.communityServiceRankConf(request.getRankType(), request.getDaoId()));
    }

    @ApiOperation(value = "历史服务中心/社区网点排行榜配置详情", notes = "历史服务中心/社区网点排行榜配置详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/historyCommunityServiceRankConfDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<EcologyPoolHistoryDaoRankConfVo> historyCommunityServiceRankConfDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolService.historyCommunityServiceRankConfDetail(request.getId()));
    }

    @ApiOperation(value = "服务中心/社区网点排行榜-数据详情", notes = "服务中心/社区网点排行榜-数据详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/communityServiceRankPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolWebCommunityServiceRankVO> communityServiceRankPage(@RequestBody @Valid EcologyPoolCommunityServiceRankRequest request) {
        return PageResult.success(ecologyPoolService.communityServiceRankPage(request.getPageNo(), request.getPageSize(), request.getRankType(), request.getDaoId()));
    }

    @ApiOperation(value = "服务中心/社区网点历史排行榜", notes = "服务中心/社区网点历史排行榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/historyCommunityServiceRankConfPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolWebHistoryCommunityServiceRankConfVO> historyCommunityServiceRankConfPage(@RequestBody @Valid EcologyPoolHistoryCommunityServiceRankConfRequest request) {
        return PageResult.success(ecologyPoolService.historyCommunityServiceRankConfPage(request.getPageNo(), request.getPageSize(), request.getRankType(), request.getDaoId()));
    }

    @ApiOperation(value = "服务中心/社区网点历史排行榜-数据详情", notes = "服务中心/社区网点历史排行榜-数据详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/historyCommunityServiceRankPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolWebCommunityServiceRankVO> historyCommunityServiceRankPage(@RequestBody @Valid IdPageRequest request) {
        return PageResult.success(ecologyPoolService.historyCommunityServiceRankPage(request.getPageNo(), request.getPageSize(), request.getId()));
    }

    @ApiOperation(value = "所属服务中心/社区网点排名", notes = "所属服务中心/社区网点排名", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myCommunityServiceRank", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<EcologyPoolWebMyCommunityServiceRankVO> myCommunityServiceRank(@RequestBody EcologyPoolMyCommunityServiceRankRequest request) {
        return Result.success(ecologyPoolService.myCommunityServiceRank(request.getRankType(), UserPool.getUser(), request.getDaoId()));
    }

    @ApiOperation(value = "服务中心/社区网点排行榜奖励", notes = "服务中心/社区网点排行榜奖励分页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/communityServiceRankPrize", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<EcologyPoolCommunityServiceRankPrizeVo>> communityServiceRankPrize(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolService.communityServiceRankPrize(request.getId()));
    }

}
